package cn.wmw.xyy.web.controller;

import cn.wmw.result.JsonResult;
import cn.wmw.result.PageList;
import cn.wmw.xyy.domain.CodeUser;
import cn.wmw.xyy.query.CodeUserQuery;
import cn.wmw.xyy.service.ICodeUserService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/codeUser")
public class CodeUserController {

    @Autowired
    public ICodeUserService codeUserService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody CodeUser codeUser){
        if(codeUser.getId()!=null){
            codeUserService.updateById(codeUser);
        }else{
            codeUserService.insert(codeUser);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        codeUserService.deleteById(id);
        return JsonResult.success();
    }




    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(codeUserService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody CodeUserQuery query){
        Page<CodeUser> page = new Page<CodeUser>(query.getPage(),query.getRows());
        page = codeUserService.selectPage(page);
        return JsonResult.success(new PageList<CodeUser>(page.getTotal(),page.getRecords()));
    }


    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(codeUserService.selectById(id));
    }

    /**
     * 根据专属注册码linkCode获取对象
     */
    @RequestMapping(value = "/code/{linkCode}",method = RequestMethod.GET)
    public JsonResult linkCode(@PathVariable("linkCode")String linkCode){
        return codeUserService.selectByLinkCode(linkCode);
    }
}
